package org.example.core.Service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.core.Service.RedisService;
import org.example.core.pojo.ComfyuiTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Slf4j
@Service
public class RedisServiceImpl implements RedisService {
    public final static String TASK_KEY_PREFIX = "task_";
    private final static String DISTRIBUTED_ID_KEY = "DISTRIBUTED_ID";
    public final static String DISTRIBUTED_QUEUE_KEY = "DISTRIBUTED_QUEUE";

    public final static String RUN_TASK_KEY="run_task_";
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ComfyuiTask addQueueTask(ComfyuiTask task) {
        //获取自增ID
        Long idx = redisTemplate.opsForValue().increment(DISTRIBUTED_ID_KEY);
        task.setIndex(idx);
        //将队列任务添加到ZSET中
        redisTemplate.opsForZSet().add(DISTRIBUTED_QUEUE_KEY,task.getId(),idx);
        //将队列任务具体信息存入redis中
        redisTemplate.opsForValue().set(TASK_KEY_PREFIX+task.getId(), JSON.toJSONString(task),Duration.ofHours(12));

        return task;
    }

    @Override
    public ComfyuiTask popQueueTask() {
        Long size = redisTemplate.opsForZSet().size(DISTRIBUTED_QUEUE_KEY);
        if(size>0){
            //返回类型为zset中的value和scope
            ZSetOperations.TypedTuple<String> stringTypedTuple = redisTemplate.opsForZSet().popMin(DISTRIBUTED_QUEUE_KEY);
            if (stringTypedTuple != null&& stringTypedTuple.getValue() != null){
                //得到zset集合中的value值
                String taskId = stringTypedTuple.getValue();
                String s = redisTemplate.opsForValue().get(TASK_KEY_PREFIX + taskId);
                //将json字符串转换为对应类型的对象
                ComfyuiTask comfyuiTask = JSON.parseObject(s, ComfyuiTask.class);
                return comfyuiTask;
            }
        }

        return null;
    }

    @Override
    public void popQueueTask(ComfyuiTask task) {
        redisTemplate.opsForZSet().remove(DISTRIBUTED_QUEUE_KEY,task.getId());
        redisTemplate.opsForValue().getAndDelete(TASK_KEY_PREFIX+task.getId());
    }

    @Override
    public void addStartTask(String prompt, ComfyuiTask task) {
        redisTemplate.opsForValue().set(RUN_TASK_KEY+prompt,JSON.toJSONString(task), Duration.ofHours(12));
    }

    @Override
    public ComfyuiTask getStartTask(String prompt) {
        String s = redisTemplate.opsForValue().get(RUN_TASK_KEY + prompt);
        if(StrUtil.isNotEmpty(s)){
            return JSON.parseObject(s, ComfyuiTask.class);
        }
        return null;
    }
    public void rmStartTask(String prompt) {
        Boolean delete = redisTemplate.delete(RUN_TASK_KEY + prompt);
        if(delete){
            System.out.println("删除成功");
        }else
            System.out.println("删除失败");
    }

        @Override
    public boolean hasQueueTask() {

        return redisTemplate.opsForZSet().size(DISTRIBUTED_QUEUE_KEY)>0;
    }
}
